// pages/home/home.js
import { 
  getMultiData,
  getGoodsData
 } from '../../service/home'

const types = ["pop", "new", "sell"]
const TOP_DISTANCE = 1000

Page({
  data: {
    banners: [],
    recommends: [],
    titles: ["流行", "新款", "精选"],
    goods: {
      'pop': { page: 0, list: [] },
      'new': { page: 0, list: [] },
      'sell': { page: 0, list: [] }
    },
    currentType: "pop",
    showBackTop: false,
    isTabFixed: false,
    tabScrollTop: 0,
  },

  onLoad: function (options) {
    this._getMultiData();

    this._getGoodsData('pop');
    this._getGoodsData('new');
    this._getGoodsData('sell');
  },

  // -------------------- 网络请求相关 --------------------------
  _getMultiData() {
    getMultiData().then(res => {
      const banners = res.data.data.banner.list;
      const recommends = res.data.data.recommend.list;
      this.setData({
        banners,
        recommends
      })
    })
  },

  _getGoodsData(type) {
    const page = this.data.goods[type].page + 1;
    getGoodsData(type, page).then(res => {
      const list = res.data.data.list;
      const oldList = this.data.goods[type].list;
      oldList.push(...list);
      const typeKey = `goods.${type}.list`;
      const pageKey = `goods.${type}.page`;
      this.setData({
        [typeKey]: oldList,
        [pageKey]: page
      })
    })
  },


  // ------------------------- 事件监听相关 ---------------------------
  handleTabClick(event) {
    const index = event.detail.index;
    this.setData({
      currentType: types[index],
    })
  },

  // handleImageLoad() {
  //   wx.createSelectorQuery().select('#tab-control').boundingClientRect(rect => {
  //     console.log(rect.top)
  //     this.data.tabScrollTop = rect.top;
  //   }).exec();
  // },

  onReachBottom() {
    this._getGoodsData(this.data.currentType);
  },

  onPageScroll(options) {
    const scrollTop = options.scrollTop;

    const flag = scrollTop >= TOP_DISTANCE;
    if (flag !== this.data.showBackTop) {
      this.setData({
        showBackTop: scrollTop >= TOP_DISTANCE
      })
    }

    // 修改 isTabFixed 属性
    const isFixedModified = scrollTop > this.data.tabScrollTop;
    if (isFixedModified !== this.data.isTabFixed) {
      this.setData({
        isTabFixed: isFixedModified
      })
    }
  }
})